<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.beginTime"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.endTime"
                style="min-width: calc(50% - 10px)"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{ $t('今日') }} </a-menu-item>
                  <a-menu-item key="2"> {{ $t('近三天') }} </a-menu-item>
                  <a-menu-item key="3"> {{ $t('近七天') }} </a-menu-item>
                  <a-menu-item key="4"> {{ $t('近一月') }} </a-menu-item>
                  <a-menu-item key="5"> {{ $t('近三月') }} </a-menu-item>
                  <a-menu-item key="6"> {{ $t('近半年') }} </a-menu-item>
                  <a-menu-item key="7"> {{ $t('本年') }} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px">
                  {{ $t('快捷查询') }} <a-icon type="down"
                /></a-button>
              </a-dropdown>
              <a-button
              v-has="'BisMemberReportController:export'"
                type="primary"
                :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('会员漫游汇总'))"
                >{{ $t('导出') }}</a-button
              >
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSources"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('详情') }}</a>
        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'SiteCollectionReport',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '会员漫游汇总页面',
      // 表头
      columns: [
        {
          title: this.$t('会员归属门店'),
          align: 'center',
          dataIndex: 'shopCode',
          customRender: (t, r) => {
            if (t) {
              return r.shopCode + r.shopName
            }
            return r.shopName
          },
        },
        {
          title: this.$t('会员数量'),
          align: 'center',
          dataIndex: 'memberCount',
        },
      ],
      url: {
        list: '/member/bisMemberReport/roam',
        exportXlsUrl: '/member/bisMemberReport/roamExportXls',
      },
      dataSources: [],
      isNoPageTotal: true,
      queryParam: {
        beginTime: moment().format('YYYY-MM-01 00:00:00'),
        endTime: moment().format('YYYY-MM-DD 23:59:59'),
      },
      isCustom: true,
      thRows: 2,
    }
  },
  created() {},
  mounted() {
    /* let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    } */
  },
  watch: {
    dataSource(nVal) {
      this.columns = [
        {
          title: this.$t('会员归属门店'),
          align: 'center',
          dataIndex: 'shopCode',
          width: 200,
          customRender: (t, r) => {
            if (t) {
              return r.shopCode + r.shopName
            }
            return r.shopName
          },
        },
        {
          title: this.$t('会员数量'),
          align: 'center',
          dataIndex: 'memberCount',
          width: 100,
        },
      ]
      if (nVal.length > 0 && nVal[0].roamList.length > 0) {
        nVal[0].roamList.forEach((pt) => {
          let dtl = {
            title: pt.shopCode+pt.shopName,
            align: 'center',
            children: [
              {
                title: this.$t('充值金额'),
                align: 'center',
                dataIndex: 'rechargePrice' + pt.shopCode,
                width: 100,
                customRender: (t, r, i) => {
                  if (!t) {
                    t = 0
                  }
                  return t.toFixed(2)
                },
                customCell: (r, i) => {
                  return {
                    style: {
                      textAlign: 'right',
                    },
                  }
                },
              },
              {
                title: this.$t('充值币值'),
                align: 'center',
                dataIndex: 'recharge' + pt.shopCode,
                width: 100,
                customRender: (t, r, i) => {
                  if (!t) {
                    t = 0
                  }
                  return t.toFixed(2)
                },
                customCell: (r, i) => {
                  return {
                    style: {
                      textAlign: 'right',
                    },
                  }
                },
              },
              {
                title: this.$t('消费币值'),
                align: 'center',
                dataIndex: 'consume' + pt.shopCode,
                width: 100,
                customRender: (t, r, i) => {
                  if (!t) {
                    t = 0
                  }
                  return t.toFixed(2)
                },
                customCell: (r, i) => {
                  return {
                    style: {
                      textAlign: 'right',
                    },
                  }
                },
              }
            ],
          }
          this.columns.push(dtl)
        })
      }
      this.dataSources = []
      this.orgData(nVal, 0)
    },
  },
  methods: {
    orgData(list, i) {
      if (i <= list.length - 1) {
        let data = {}
        data.shopCode = list[i].shopCode
        data.shopName = list[i].shopName
        data.memberCount = list[i].memberCount
        this.dataSources.push(data)
        this.orgData1(list, 0, i)
      }
    },
    orgData1(list, i, j) {
      if (i <= list[j].roamList.length - 1) {
        list[j].roamList.forEach((lt) => {
          this.$set(this.dataSources[j], 'rechargePrice' + lt.shopCode, lt.rechargePrice)
          this.$set(this.dataSources[j], 'recharge' + lt.shopCode, lt.recharge)
          this.$set(this.dataSources[j], 'consume' + lt.shopCode, lt.consume)
        })
        i++
        this.orgData1(list, i, j)
      } else if (j < list.length - 1) {
        j++
        this.orgData(list, j)
      }else{
        console.log(this.dataSources)
      }
    },
    initDictConfig() {
      this.searchQuery()
    },
    customRow(r, i) {
      return {
        on: {
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.shopName == this.$t('合计') ? 700 : 400,
        },
      }
    },
    searchReset() {
      this.queryParam = {
        beginTime: moment().format('YYYY-MM-01 00:00:00'),
        endTime: moment().format('YYYY-MM-DD 23:59:59'),
        shopCode: '',
      }
      this.searchQuery()
    },
    handleDateClick(evt) {
      if (evt.key == '1') {
        this.queryParam.beginTime = moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '2') {
        this.queryParam.beginTime = moment().subtract(2, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '3') {
        this.queryParam.beginTime = moment().subtract(6, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '4') {
        this.queryParam.beginTime = moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '5') {
        this.queryParam.beginTime = moment().subtract(3, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '6') {
        this.queryParam.beginTime = moment().subtract(6, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '7') {
        this.queryParam.beginTime = moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>